package world.hezi.handlers;

import net.mamoe.mirai.contact.Contact;
import net.mamoe.mirai.event.EventHandler;
import net.mamoe.mirai.event.ListeningStatus;
import net.mamoe.mirai.event.SimpleListenerHost;
import net.mamoe.mirai.event.events.MessageEvent;
import net.mamoe.mirai.message.data.Image;
import net.mamoe.mirai.message.data.Message;
import net.mamoe.mirai.message.data.MessageChain;
import net.mamoe.mirai.message.data.MessageChainBuilder;
import org.jetbrains.annotations.NotNull;
import org.opencv.imgcodecs.Imgcodecs;
import world.hezi.entity.SIFTResponse;
import world.hezi.utils.OpenCvUtil;
import world.hezi.utils.PicturesUtil;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;


/**
 * @author wangzijian
 * @since 2023/11/8 16:31
 */
public class FxxkLongHandlers  extends SimpleListenerHost {

    private static final List<Long> ALLOW = Arrays.asList(651343119L
            , 649538504L
            , 981566412L
            ,730451715L
            ,247133571L
            ,790611872L
            ,861485420L
//            ,655620062L
    );
    @NotNull
    @EventHandler
    public ListeningStatus onMessage(@NotNull MessageEvent event) {
        if (!ALLOW.contains(event.getSource().getSubject().getId())){
            return ListeningStatus.LISTENING;
        }
        Message metaMessage = event.getMessage();
        Contact contact = event.getSubject();
        List<String> idsList = PicturesUtil.getIdsList(metaMessage);
        for (String s : idsList) {
            File file = PicturesUtil.saveAndGetFile(s);
            SIFTResponse siftResponse = OpenCvUtil.analysisSIFT(file, new File("C:\\Users\\65134\\Desktop\\long\\face"));
            MessageChainBuilder messages = new MessageChainBuilder();
            String out = "C:\\Users\\65134\\Desktop\\long\\out\\" + UUID.randomUUID() + ".png";
            if (siftResponse != null) {
                Imgcodecs.imwrite(out,siftResponse.getMat());
                Image image = PicturesUtil.uploadAndGetImage(new File(out), contact);
                if (image != null){
                    double degree = ((double)siftResponse.getGoodMatches() / (double)siftResponse.getAllMatches())*100;
                    System.out.println("degree:"+degree);
                    if (degree > 50 && siftResponse.getAllMatches() > 4){
                        MessageChain build = messages
                                .append("Degree of matching：").append(String.valueOf(degree)).append("%")
                                .append(image)
                                .append("\n")
                                .append(siftResponse.toString())
                                .build();
                        contact.sendMessage(build);
                    }
                }
            }

        }
        return ListeningStatus.LISTENING;
    }
}
